/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013年9月11日
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.service.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.sys.dao.UserDAO;
import com.hnas.sys.orm.User;
import com.hnas.sys.service.system.dto.DTOUser;
import com.hnas.sys.service.system.mapper.UserMapper;

/**
 * @author 郑兴(zhengxing)
 *
 */
@Component("userService")
@Transactional
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDAO userDAO;
//	@Autowired
//	AgreementDAO agreementDAO;
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#findByUserName(java.lang.String)
	 */
	public DTOUser findByUserName(String userName) {
		User user = userDAO.findByUserName(userName);
		return UserMapper.getInstance().mapToValue(user);
	}
	public List<DTOUser> getUsersWithRole(String roleName) {
		List<User> user = userDAO.getUsersWithRole(roleName);
		return UserMapper.getInstance().mapToValues(user);
	}
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#loadAll()
	 */
//	public List<DTOUser> loadAll() {
//		String hql = "from User t ";
//		return UserMapper.getInstance().mapToValues(userDAO.findByQuery(hql));
//	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#getUserByAccount(java.lang.String)
	 */
	public DTOUser getUserByAccount(String account) {
		DTOUser dtoUser = null;
		User user = userDAO.getUserByAccount(account);
		if (user != null) {
			return UserMapper.getInstance().mapToValue(user);
		} else {
			return dtoUser;
		}

	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#getUserById(java.lang.Long)
	 */
	public DTOUser getUserById(Long id) {
		DTOUser dtoUser = null;
		User user = userDAO.find(id);
		if (user != null) {
			return UserMapper.getInstance().mapToValue(user);
		} else {
			return dtoUser;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#updateUser(com.hna.adt.service.user.dto.DTOUser)
	 */
//	public void updateUser(DTOUser dtoUser, long id) {
//		User entity = UserMapper.getInstance().mapToEntity(dtoUser);
////		entity.setUserType(UserType.MANAGER);
//		userDAO.merge(entity);
////		
////		List<Agreement> list = agreementDAO.getAgreementsByUser(dtoUser.getAccount());
////		if (list != null && list.size() > 0) {
////			Agreement dto = list.get(0);
////			dto.setApplyer(null);
////			agreementDAO.merge(dto);
////		}
////		
////		Agreement agreement_modify = agreementDAO.find(id);
////		if (agreement_modify != null) {
////			if (agreement_modify.getApplyer() != null) {
////				User user = agreement_modify.getApplyer();
////				user.setCompany("");
////				userDAO.merge(user);
////			}
////			agreement_modify.setApplyer(entity);
////			agreementDAO.merge(agreement_modify);
////		}
//	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#updateUser(com.hna.adt.service.user.dto.DTOUser)
	 */
//	public void updateUser(DTOUser dtoUser) {
//		User entity = UserMapper.getInstance().mapToEntity(dtoUser);
////		entity.setUserType(UserType.MANAGER);
//		userDAO.merge(entity);
//	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#add(com.hna.adt.service.user.dto.DTOUser)
	 */
//	public void add(DTOUser user, long id) {
//		User entity = UserMapper.getInstance().mapToEntity(user);
//		if (null != user && null == user.getId()  || user.getId()==0) {
////			entity.setUserType(UserType.MANAGER);
//			userDAO.persist(entity);
////			Agreement agreement_modify = agreementDAO.find(id);
////			if (agreement_modify != null) {
////				if (agreement_modify.getApplyer() != null) {
////					User user_modify = agreement_modify.getApplyer();
////					user_modify.setCompany("");
////					userDAO.merge(user_modify);
////				}
////				agreement_modify.setApplyer(entity);
////				agreementDAO.merge(agreement_modify);
////			}
//		}
//	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#deleteUser(java.lang.Long)
	 */
//	public void deleteUser (Long id) {
//		User user = userDAO.find(id);
//		
//		if (user != null) {
////			List<Agreement> list = agreementDAO.getAgreementsByUser(user.getAccount());
////			if (list != null && list.size() > 0) {
////				Agreement dto = list.get(0);
////				dto.setApplyer(null);
////				agreementDAO.merge(dto);
////			}
//			user.setStatus(ResourceStatus.INVALID);
////			userDAO.remove(user);
//			this.userDAO.merge(user);
//		}
//	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.service.user.UserService#delete(java.lang.Long)
	 */
//	public void delete (Long id) {
//		User user = userDAO.find(id);
//		if (user != null) {
//			user.setStatus(ResourceStatus.INVALID);
//			userDAO.merge(user);//.remove(user);
//		}
//	}

//	@Override
//	public List<DTOUser> getUserWithRoleAdmin() {
//		return UserMapper.getInstance().mapToValues(userDAO.getUserWithRoleAdmin());
//	}

	
	public List<DTOUser> getUserWithRole(String account, String roleCode) {
		return UserMapper.getInstance().mapToValues(userDAO.getUserWithRole(account, roleCode));
	}
	public List<DTOUser> getUserByAgencyid(long Agencyid){
		List<User> users = userDAO.getUserByAgencyid(Agencyid);
		return UserMapper.getInstance().mapToValues(users);
	}

//	public long updateFrontUser(DTOUser user) {
//		// TODO Auto-generated method stub
////		String h
//	return 0;
//	}

//	public DTOPagedList<DTOUser> search(DTOSearchUser con) {
//		// TODO Auto-generated method stub
////		return null;
//		PageBean<User> page = new PageBean<User>();
//		page.setPageNo(con.getPageNo());
//		page.setPageSize(con.getPageSize());
//		page.setTotalCount(con.getTotalCount());
//		StringBuffer sb = new StringBuffer();
//		List<String> paras = new ArrayList<String>();
//		int j=1;
////		sb.append(" and t.userType=?" + j++);
////		paras.add(UserType.MANAGER);
////		if (null!=con.getDto()){
//		if (StringUtil.isNotEmpty(con.getName())) {
//			sb.append(" and t.name like ?" + j++);
//			paras.add("%"+con.getName()+"%");
//		}
////		}
//		if (sb.length() > 0) {
//			sb.delete(0, 5);
//			sb.insert(0, " from User t where ");
//		} else {
//			sb.append(" from User t");
//		}
//		page.setQuery(sb.toString());
//		page.setQueryCount("select count(t.id) " + sb);
////		page.setQuery(sb.insert(" from User t".length(), " join fetch t.product ").toString());
//		this.userDAO.queryByPage(page, paras.toArray());
//		DTOPagedList<DTOUser> pageList = new DTOPagedList<DTOUser>();
//		pageList.setPageNo(page.getPageNo());
//		pageList.setPageSize(page.getPageSize());
//		pageList.setTotalCount(page.getTotalCount());
//		List<DTOUser> ls = new ArrayList<DTOUser>();
//		for (User or : page.getList()) {
//			DTOUser dto = new DTOUser();
//			dto = UserMapper.getInstance().mapToValue(or);
//			
//			ls.add(dto);
//		}
//		pageList.setList(ls);
//		return pageList;
//	}

//	public void disable(long id) {
//		// TODO Auto-generated method stub
//		User vo = this.userDAO.find(id);
//		vo.setStatus(ResourceStatus.INVALID);
//		this.userDAO.merge(vo);
//	}

	
}
